/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.ResourceTable;
import com.transitionseverywhere.ChangeImageTransform;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;


public class ListAbilitySlice extends AbilitySlice {
    private ListContainer listContainer;

    private MainListProvider listItemProvider;

    private ArrayList<String> listData;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        System.out.println("Before setUI content" );

        listData = new ArrayList<>();
        listData.add("Simple animations with AutoTransition ");
        listData.add("interpolator, duration, start delay ");
        listData.add("Path motion ");
        listData.add("Slide transition ");
        listData.add("Scale transition ");
        listData.add("Explode transition and epicenter ");
        listData.add("Transition names ");
        listData.add("ChangeImage transform transition ");
        listData.add("Recolor transition ");
        listData.add("Rotate transition ");
        listData.add("Change text transition ");
        listData.add("Custom transition ");


        ComponentContainer root_layout = (ComponentContainer) LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_root_list, null, false);
        ComponentContainer mRootId = (ComponentContainer) root_layout.findComponentById(ResourceTable.Id_parentid);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(255, 0, 0));
        background_color_element.setBounds(0, 0, mRootId.getRight(), mRootId.getBottom());
        mRootId.setBackground(background_color_element);


        listContainer = (ListContainer) root_layout.findComponentById(ResourceTable.Id_lc_root);
        listItemProvider = new MainListProvider(this, listData);
        listItemProvider.getCount();
        listContainer.setItemProvider(listItemProvider);
        listContainer.setReboundEffect(true);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
            }
        });

        super.setUIContent(root_layout);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    public class MainListProvider extends RecycleItemProvider {
        private List<String> listData;

        private Context context;

        MainListProvider(Context context, ArrayList<String> listData) {
            this.listData = listData;
            this.context = context;
        }

        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            Component container = getItemComponent(i);
            return container;
        }

        private Component getItemComponent(int position) {
            Component container = getComponent(position);
            return container;
        }

        private Component getComponent(int position) {
            String name = listData.get(position);
            Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item2, null, false);
            Text nameText = (Text) container.findComponentById(ResourceTable.Id_name);
            nameText.setText(name);
            nameText.setClickedListener(component -> launchTestCase(name, position));
            return container;
        }

        private void launchTestCase(String name, int position) {
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ABILITY_NEW_MISSION);
            switch (position) {
                case 0:
                    present(new AutoTransitionSlice(), intent);
                    break;
                case 1:
                    present(new InterPolatorSample(), intent);
                    break;
                case 2:
                    present(new PathMotionSample(), intent);
                    break;
                case 3:
                    present(new SlideSample(), intent);
                    break;
                case 4:
                    present(new ScaleSlice(), intent);
                    break;
                case 5:
                    present(new ExplodeAndEpicenterSlice(), intent);
                    break;
                case 6:
                    present(new TransitionNamesSlice(), intent);
                    break;
                case 7:
                    present(new ImageTransformSlice(), intent);
                    break;
                case 8:
                    present(new RecolorSample(), intent);
                    break;
                case 9:
                    present(new RotationSlice(), intent);
                    break;
                case 10:
                    present(new ChangeTextSlice(), intent);
                    break;
                case 11:
                    present(new CustomTransitionSlice(), intent);
                    break;
            }

        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }


}
